package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.mapper.AnnouncementMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.BooleanOperators;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnnouncementApiImpl implements AnnouncementApi {
    @Autowired
    private AnnouncementMapper announcementMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageResult<Announcement> findPage(Long page, Long pageSize) {
        //返回的结果
        PageResult<Announcement> pageResult = new PageResult<>();
        //分页需要的参数
        Page<Announcement> announcementPage = new Page<>(page, pageSize);

        IPage<Announcement> mapIPage = announcementMapper.selectPage(announcementPage, null);
        pageResult.setPages(page);
        pageResult.setPagesize(pageSize);
        pageResult.setPages(mapIPage.getPages());
        pageResult.setItems(mapIPage.getRecords());
        pageResult.setCounts(mapIPage.getTotal());
        return pageResult;
    }

    @Override
    public void addFriend(Long longInUserId, Integer userId) {
        Friend friend = new Friend();
        long timeMillis = System.currentTimeMillis();
        friend.setCreated(timeMillis);
        friend.setFriendId(userId.longValue());
        friend.setUserId(longInUserId);
        //判断是否有好友
        Query query = new Query(Criteria.where("userId").is(longInUserId).and("friendId").is(userId));
        if (!mongoTemplate.exists(query,Friend.class)){
        mongoTemplate.insert(friend);
        }
        Query toQuery = new Query(Criteria.where("userId").is(userId).and("friendId").is(longInUserId));
        if (!mongoTemplate.exists(toQuery,Friend.class)){
        Friend tofriend = new Friend();
        friend.setCreated(timeMillis);
        friend.setFriendId(longInUserId);
        friend.setUserId(userId.longValue());
        mongoTemplate.insert(tofriend);
        }
    }

    @Override
    public PageResult pageLoves(Long page, Long pagesize, Long loginUserId, int type) {
        Query query = new Query(Criteria.where("targetUserId").is(loginUserId).and("commentType").is(type));
        long total = mongoTemplate.count(query, Comment.class);
        List<Comment> userComment=new ArrayList<>();
        if (total>0){
            query.skip((page-1)*pagesize).limit(pagesize.intValue());
            query.with(Sort.by(Sort.Order.desc("created")));
        userComment = mongoTemplate.find(query, Comment.class);
        }
        return PageResult.pageResult(page,pagesize,userComment,total);
    }
}
